<template>
  <el-dialog v-model="visible" :title="!dataForm.configId ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <!-- <el-form-item label="原材料配置的唯一标识符" prop="configId">
        <el-input v-model="dataForm.configId" placeholder="原材料配置的唯一标识符"></el-input>
      </el-form-item> -->
      <el-form-item label="菜品ID" prop="dishId">
        <el-input v-model="dataForm.dishId" placeholder="菜品ID，关联到菜单表中的菜品"></el-input>
      </el-form-item>
      <el-form-item label="原材料名称" prop="materialName">
        <el-input v-model="dataForm.materialName" placeholder="原材料名称"></el-input>
      </el-form-item>
      <el-form-item label="原材料规格" prop="materialSpecification">
        <el-input v-model="dataForm.materialSpecification" placeholder="原材料的规格，使用JSON格式存储"></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="unitOfMeasure">
        <el-input v-model="dataForm.unitOfMeasure" placeholder="计量单位，如克、升、件等"></el-input>
      </el-form-item>
      <el-form-item label="单位数量" prop="quantity">
        <el-input v-model="dataForm.quantity" placeholder="每单位菜品所需的原材料数量"></el-input>
      </el-form-item>
      <el-form-item label="供应商ID" prop="supplierId">
        <el-input v-model="dataForm.supplierId" placeholder="供应商ID，关联到供应商表"></el-input>
      </el-form-item>
      <el-form-item label="质量标准" prop="qualityStandard">
        <el-input v-model="dataForm.qualityStandard" placeholder="原材料应满足的质量标准"></el-input>
      </el-form-item>
      <el-form-item label="单价" prop="costPerUnit">
        <el-input v-model="dataForm.costPerUnit" placeholder="原材料的单价"></el-input>
      </el-form-item>
      <el-form-item label="总成本" prop="totalCost">
        <el-input v-model="dataForm.totalCost" placeholder="原材料的总成本，基于数量和单价计算得出"></el-input>
      </el-form-item>
      <el-form-item label="替代瓶" prop="alternativeMaterials">
        <el-input v-model="dataForm.alternativeMaterials" placeholder="可替代的原材料列表，使用JSON格式存储"></el-input>
      </el-form-item>
      <el-form-item label="保质期" prop="expirationDate">
        <el-date-picker type="date" placeholder="原材料的有效期或保质期" v-model="dataForm.expirationDate" format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"></el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input v-model="dataForm.notes" placeholder="关于原材料的任何附加说明或备注"></el-input>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker type="datetime" placeholder="记录原材料配置创建的时间" v-model="dataForm.createdAt"
          format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker type="datetime" placeholder="记录原材料配置最后更新的时间" v-model="dataForm.updatedAt"
          format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item> -->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  configId: "",
  dishId: "",
  materialName: "",
  materialSpecification: "",
  unitOfMeasure: "",
  quantity: "",
  supplierId: "",
  qualityStandard: "",
  costPerUnit: "",
  totalCost: "",
  alternativeMaterials: "",
  expirationDate: "",
  notes: "",
  createdAt: "",
  updatedAt: "",
});

const rules = ref({
});

const init = (configId?: number) => {
  visible.value = true;
  dataForm.configId = configId;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (configId) {
    getInfo(configId);
  }
};

// 获取信息
const getInfo = (configId: number) => {
  baseService.get("/sys/rawmaterialsconfig/" + configId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.configId ? baseService.post : baseService.put)("/sys/rawmaterialsconfig", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>